
package com.multimedia.room.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.multimedia.room.BaseActivity;
import com.multimedia.room.CommandManager;
import com.multimedia.room.GroupInfo;
import com.multimedia.room.ICleanListener;
import com.multimedia.room.MediaMessage;
import com.multimedia.room.R;

public class TeachMonitorFragment extends Fragment implements ICleanListener {
    private static final String TAG = "MMClass/TeachIntercomFragment";
    private TextView mStatusView;
    private static boolean sIsIntercom = false;
    private static MediaMessage mCurrentMessage;
    private static String mGroupAddr;

    public static TeachMonitorFragment newInstance(MediaMessage message) {
        mCurrentMessage = message;
        TeachMonitorFragment fragment = new TeachMonitorFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.teach_intercom_layout, null);
        mStatusView = (TextView) rootView.findViewById(R.id.intercom_standard_status);
        if (mCurrentMessage.isMe()) {
            mStatusView.setText(R.string.receive_talk_teacher);
            CommandManager.startSat();
            CommandManager.openMic();
            CommandManager.closeHeadSet();
            CommandManager.leaveGroup(mGroupAddr);
            mGroupAddr = GroupInfo.getGroupId(mCurrentMessage.getGroup());
            CommandManager.setIntercom(mGroupAddr);
            CommandManager.micLoopbackS(false);
        }else{
            sIsIntercom = false;
            CommandManager.closeMic();
            mStatusView.setText(R.string.receive_demonstrate);
        }
        if(this.getActivity() instanceof BaseActivity){
            ((BaseActivity)this.getActivity()).registerCleanListener(this);
        }
        return rootView;
    }

    @Override
    public void clean() {
        CommandManager.leaveGroup(mGroupAddr);
        CommandManager.joinGroup("230.1.2.3");
        CommandManager.closeMic();
        CommandManager.openHeadSet();
        mStatusView.setText(R.string.receive_teacher_demonstrate);
    }

    @Override
    public void allClean() {
        CommandManager.leaveGroup(mGroupAddr);
        CommandManager.joinGroup("230.1.2.3");
        CommandManager.closeMic();
        CommandManager.openHeadSet();

        mStatusView.setText(R.string.receive_teacher_demonstrate);
    }
    @Override
    public void onDetach() {
        super.onDetach();
        ((BaseActivity) this.getActivity()).unregisterCleanListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
